package soar.test.bytebuffer;

import java.nio.ByteBuffer;

public class App
{
    public static void main(String[] args)
    {
        new App().run();
    }

    private void run()
    {
        try
        {
            String split = "========================================";
            System.out.println(split);

            String s = "春满人间世，日照大旗红。携卷登高唱，流韵壮东风。";

            ByteBuffer bb = ByteBuffer.wrap(s.getBytes());
            System.out.println(String.format("wrap: "
                    + "capacity(%d) remaining(%d) position(%d) limit(%d)",
                    bb.capacity(), bb.remaining(), bb.position(), bb.limit()));
            
            System.out.println(split);

            // get: position+=len
            byte[] buf = new byte[bb.remaining()];
            bb.get(buf);
            System.out.println("get: "+new String(buf));
            System.out.println(String.format("get: "
                    + "capacity(%d) remaining(%d) position(%d) limit(%d)",
                    bb.capacity(), bb.remaining(), bb.position(), bb.limit()));
            
            System.out.println(split);

            // clear: position=0 limit=capacity
            bb.clear();
            System.out.println(String.format("clear: "
                    + "capacity(%d) remaining(%d) position(%d) limit(%d)",
                    bb.capacity(), bb.remaining(), bb.position(), bb.limit()));
            bb.get(buf);
            System.out.println("get: "+new String(buf));
            System.out.println(String.format("get: "
                    + "capacity(%d) remaining(%d) position(%d) limit(%d)",
                    bb.capacity(), bb.remaining(), bb.position(), bb.limit()));
            
            System.out.println(split);

            // flip: position=0 limit=position
            bb.flip();
            System.out.println(String.format("flip: "
                    + "capacity(%d) remaining(%d) position(%d) limit(%d)",
                    bb.capacity(), bb.remaining(), bb.position(), bb.limit()));
            bb.flip();
            System.out.println(String.format("flip: "
                    + "capacity(%d) remaining(%d) position(%d) limit(%d)",
                    bb.capacity(), bb.remaining(), bb.position(), bb.limit()));
            bb.clear();
            System.out.println(String.format("clear: "
                    + "capacity(%d) remaining(%d) position(%d) limit(%d)",
                    bb.capacity(), bb.remaining(), bb.position(), bb.limit()));
            
            System.out.println(split);

            // put -> flip -> get -> clear -> put
            bb = ByteBuffer.allocate(1024);
            bb.put(s.getBytes());
            System.out.println(String.format("put: "
                    + "capacity(%d) remaining(%d) position(%d) limit(%d)",
                    bb.capacity(), bb.remaining(), bb.position(), bb.limit()));
            bb.flip();
            System.out.println(String.format("flip: "
                    + "capacity(%d) remaining(%d) position(%d) limit(%d)",
                    bb.capacity(), bb.remaining(), bb.position(), bb.limit()));
            bb.get(buf);
            System.out.println("get: "+new String(buf));
            System.out.println(String.format("get: "
                    + "capacity(%d) remaining(%d) position(%d) limit(%d)",
                    bb.capacity(), bb.remaining(), bb.position(), bb.limit()));
            bb.clear();
            System.out.println(String.format("clear: "
                    + "capacity(%d) remaining(%d) position(%d) limit(%d)",
                    bb.capacity(), bb.remaining(), bb.position(), bb.limit()));
            bb.put(s.getBytes());
            System.out.println(String.format("put: "
                    + "capacity(%d) remaining(%d) position(%d) limit(%d)",
                    bb.capacity(), bb.remaining(), bb.position(), bb.limit()));
            
            System.out.println(split);

            System.out.println("扩展capacity");
            System.out.println();
            
            bb = ByteBuffer.allocate(100);
            bb.put(s.getBytes());
            System.out.println(String.format("bb1 put: "
                    + "capacity(%d) remaining(%d) position(%d) limit(%d)",
                    bb.capacity(), bb.remaining(), bb.position(), bb.limit()));
            bb.flip();
            System.out.println(String.format("bb1 flip: "
                    + "capacity(%d) remaining(%d) position(%d) limit(%d)",
                    bb.capacity(), bb.remaining(), bb.position(), bb.limit()));
            buf = new byte[bb.limit()];
            bb.get(buf);
            ByteBuffer bb2 = ByteBuffer.allocate(200);
            bb2.put(buf);
            System.out.println(String.format("bb2 put: "
                    + "capacity(%d) remaining(%d) position(%d) limit(%d)",
                    bb2.capacity(), bb2.remaining(), bb2.position(),
                    bb2.limit()));
            
            System.out.println(split);
        }
        catch (Exception e)
        {
            System.err.println(String.format("%s", e));
        }
    }

}
